﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Win32;
using Xceed.Wpf.Toolkit.PropertyGrid;
using Xceed.Wpf.Toolkit.PropertyGrid.Editors;

namespace SpectrumAnalyser.Shell.PropertyGrid.Editors
{
    public class FilepathEditor : ITypeEditor
    {
        public FrameworkElement ResolveEditor(PropertyItem propertyItem)
        {
            var root = new Grid();
            root.ColumnDefinitions.Add(new ColumnDefinition {Width = new GridLength(1, GridUnitType.Star)});
            root.ColumnDefinitions.Add(new ColumnDefinition {Width = new GridLength(1, GridUnitType.Auto)});

            var textBox = new TextBox();

            //create the binding from the bound property item to the editor
            var binding = new Binding("Value")
                               {
                                   Source = propertyItem,
                                   ValidatesOnExceptions = true,
                                   ValidatesOnDataErrors = true,
                                   UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                                   Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay
                               };
            BindingOperations.SetBinding(textBox, TextBox.TextProperty, binding);

            root.Children.Add(textBox);
            Grid.SetColumn(textBox, 0);

            var button = new Button {Content = "...", Height = 25, Width = 30};
            button.Click += (sender, args) =>
                                {
                                    OpenFileDialog dialog = null;
                                    try
                                    {
                                        var info = new FileInfo(textBox.Text);
                                        if (info.Exists)
                                        {
                                            dialog = new OpenFileDialog { InitialDirectory = info.DirectoryName, FileName = info.Name };
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        if (ReferenceEquals(dialog, null))
                                        {
                                            dialog = new OpenFileDialog();
                                        }
                                    }

                                    if (dialog.ShowDialog() == true)
                                    {
                                        textBox.Text = dialog.FileName;
                                        var bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
                                        if (bindingExpression != null)
                                        {
                                            bindingExpression.UpdateSource();
                                        }
                                    }
                                };

            root.Children.Add(button);
            Grid.SetColumn(button, 1);

            return root;
        }
    }
}